package com.srit.map.web.action;

import java.io.FileInputStream;
import java.io.InputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.springframework.beans.factory.annotation.Autowired;

import com.srit.core.struts2.action.BaseAction;
import com.srit.map.business.bean.CompanyDescription;
import com.srit.map.business.service.CompanyService;

public class UploadAction extends BaseAction{
	@Autowired(required = true)
	private CompanyService companyService;
	//private File file;
	public String page(){
		System.out.print("user");
		//ExcelHandle.readExcel("D:/workspace/industry-map/src/main/webapp/file/upload/abc.xls");
		
		return "jump";
	}
	public String excel(){
//		String root=ServletActionContext.getRequest().getRealPath("/upload");
		//ExcelHandle.readExcel(file);
		System.out.print("user");
//		DataBaseImport s = new DataBaseImport();
//		companyService.InsertCompany(s.readExcel("E:/abc.xls"));
		return "jump";
	}
	
	public String readExcel(){
		CompanyDescription com = new CompanyDescription();
    	Workbook rwb = null;
        try
        {
        	//String root=ServletActionContext.getRequest().getRealPath("/admin");
        	String filePath="abc.xls";
            InputStream is = new FileInputStream(filePath);
            rwb = Workbook.getWorkbook(is);
            //Sheet st = rwb.getSheet("0")这里有两种方法获取sheet表,1为名字，而为下标，从0开始
            int sheets = rwb.getNumberOfSheets();
            //System.out.println(sheets);
            Sheet st = rwb.getSheet(0);
            //System.out.print(st.getColumns());
            int rows = st.getRows();
            System.out.print(st.getRows());
            for(int i = 1 ; i < rows ; i++ ){
            Cell c00 = st.getCell(2,i);//后面个参数是行
            Cell c01 = st.getCell(3,i);//法人代表
            Cell c02 = st.getCell(4,i);//住所
            Cell c03 = st.getCell(5,i);//经营范围
            Cell c04 = st.getCell(6,i); //成立日期
            Cell c05 = st.getCell(7,i); //注册资本
            Cell c06 = st.getCell(11,i);  //企业类型
            Cell c07 = st.getCell(9,i);  //注册货币
            Cell c08 = st.getCell(0,i);  //法人代码
            
            com.setName(c00.getContents());
            com.setLaw_person(c01.getContents());
            com.setAddress(c02.getContents());
            String x = "";
            String y = "";
            x = PositionGet.getX(c02.getContents());
            y = PositionGet.getY(c02.getContents());
            float lat = 0;
            float plong = 0;
            if(!x.equals("")){
            	plong = Float.parseFloat(x);
	            lat = Float.parseFloat(y);
            }
            com.setPost_lat(lat);
            com.setPost_long(plong);
            com.setBusiness_type(c03.getContents());
            com.setRegistered_dateStr(c04.getContents());
            com.updateDate();
            System.out.print("日期");
            System.out.print(com.getRegistered_date());
            System.out.print(com.getRegistered_dateStr());
            com.setRegistered_fund(c05.getContents());
            com.setCompany_type(c06.getContents());
            com.setCurrency(c07.getContents());
            com.setCertificate(c08.getContents());
            companyService.InsertCompany(com);
            }
            //通用的获取cell值的方式,返回字符串
            //关闭
            
            rwb.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
		return "jump";
	}
	
	public void prepareReadExcel(){
		System.out.print("qqq");
	}
}
